package com.lanou.moving.fragment;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.lanou.moving.R;
import com.lanou.moving.adapter.SongListGridViewAdapter;
import com.lanou.moving.base.BaseFragment;
import com.lanou.moving.constant.URL;
import com.lanou.moving.entity.SongList;
import com.lanou.moving.utils.SwipeRefreshLoadingLayout;
import com.lanou.moving.volley.StringRequestUtf;
import com.lanou.moving.volley.VolleySingleton;
import java.util.List;

/**
 * Created by 刘琪 on 15/10/27.
 */
public class SongListHotFragment extends BaseFragment
        implements SwipeRefreshLoadingLayout.OnLoadListener,
        SwipeRefreshLoadingLayout.OnRefreshListener, AdapterView.OnItemClickListener {

    private VolleySingleton volleySingleton;
    private int page = 1;
    private List<SongList.DataEntity> dataEntities;
    private SongListGridViewAdapter adapter;
    private GridView song_list_hot_gv;
    private String url_start;
    private String url_end;
    private SwipeRefreshLoadingLayout song_list_hot_loading_layout;

    public static BaseFragment getSongListHotFragment (String url_start, String url_end) {
        BaseFragment f = new SongListHotFragment();
        Bundle arg = new Bundle();
        arg.putString("url_start", url_start);
        arg.putString("url_end", url_end);
        f.setArguments(arg);
        return  f;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_song_list_hot, null);
        song_list_hot_gv = (GridView) view.findViewById(R.id.song_list_hot_gv);
        song_list_hot_gv.setOnItemClickListener(this);
        song_list_hot_loading_layout = (SwipeRefreshLoadingLayout)
                view.findViewById(R.id.song_list_hot_loading_layout);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        volleySingleton = VolleySingleton.getInstance(mContext);
        init();
        initData();
    }

    public void refreshData(String url) {
        StringRequestUtf stringRequestUtf = new StringRequestUtf(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                SongList songList = gson.fromJson(response, SongList.class);
                List<SongList.DataEntity> datas = songList.getData();
                adapter.refreshDatas(datas);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(stringRequestUtf);
    }

    private void initData() {
        url_start = URL.SONG_LIST_HOT_START;
        url_end = URL.SONG_LIST_HOT_END;
        StringRequestUtf stringRequestUtf = new StringRequestUtf(url_start + 1 + url_end,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Gson gson = new Gson();
                        SongList songList = gson.fromJson(response, SongList.class);
                        dataEntities = songList.getData();
                        adapter = new SongListGridViewAdapter(getActivity(), dataEntities);
                        song_list_hot_gv.setAdapter(adapter);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(stringRequestUtf);
    }


    private void init() {
        // 取消点击效果
        song_list_hot_gv.setSelector(new ColorDrawable(Color.TRANSPARENT));
        song_list_hot_loading_layout.setOnRefreshListener(this);
        song_list_hot_loading_layout.setOnLoadListener(this);
        // 设置刷新效果为透明色
        song_list_hot_loading_layout.setTopColor(R.color.transparent,
                R.color.transparent, R.color.transparent, R.color.transparent);
        song_list_hot_loading_layout.setBottomColor(R.color.theme, R.color.white,
                R.color.theme, R.color.white);
    }

    public void changeUrl(String url_start, String url_end) {
        this.url_start = url_start;
        this.url_end = url_end;
        page = 1;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    public void onLoad() {
        page++;
        String url = url_start + page + url_end;
        StringRequestUtf stringRequestUtf = new StringRequestUtf(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        song_list_hot_loading_layout.setLoading(false);
                        Gson gson = new Gson();
                        SongList songSheet = gson.fromJson(response, SongList.class);
                        dataEntities = songSheet.getData();
                        adapter.addDatas(dataEntities);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(stringRequestUtf);
    }

    @Override
    public void onRefresh() {
        song_list_hot_loading_layout.setRefreshing(false);
    }
}
